package wowmobile;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatabaseTest {
    public DatabaseTest(){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "";

        try {

            String nazwa = "Test";
            con = DriverManager.getConnection(url, user, password);

            pst = con.prepareStatement("INSERT INTO testowa_tabela(`nazwa`, `id`) VALUES(?, ?)");
            //
            pst.setString(1, nazwa);
            pst.setInt(2, 123);
            pst.executeUpdate();


            pst = con.prepareStatement("SELECT * FROM testowa_tabela");
            rs = pst.executeQuery();

            while (rs.next()) {
                System.out.print(rs.getInt(1));
                System.out.print(": ");
                System.out.println(rs.getString(2));
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(DatabaseTest.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {

            try {
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(DatabaseTest.class.getName());
                lgr.log(Level.SEVERE, ex.getMessage(), ex);
            }
        }

    }
}
